/**
 * 逻辑工具类
 */
(function () {
	var _LogicTool = function () {
	};
	
	// DIV动态下拉菜单
	_LogicTool.prototype.divDropDownPostion = function (targetId, menuId, dropId) {
		var targetOffset = $("#" + targetId).offset();
		var left = 0;
		var top = 0;
		var targetOffset = $("#" + targetId).offset();
		if (($(window).height() - (targetOffset.top - $(window).scrollTop()) - $("#" + targetId).height()) > $("#" + menuId).height()) {
			top = targetOffset.top + $("#" + targetId).height();
			left = targetOffset.left;
		} else {
			if ((targetOffset.top - $(window).scrollTop()) > $("#" + menuId).height()) {
				top = targetOffset.top - $("#" + menuId).height();
				left = targetOffset.left;
			} else {
				if ($(window).height() > $("#" + menuId).height()) {
					top = ($(window).height() - $("#" + menuId).height()) / 2 + $(window).scrollTop();
					left = targetOffset.left + $("#" + targetId).width() + 5;
				} else {
					top = $(window).scrollTop() + 10;
					left = targetOffset.left + $("#" + targetId).width() + 5;
					$("#" + dropId).css("height", $(window).height() - 25).css("overflow-y", "auto").css("overflow-x", "hidden");
				}
			}
		}
		$("#" + menuId).offset({left:left, top:top});
	};
	
	// 逻辑增加(大按钮)下拉菜单
	_LogicTool.prototype.showLogicTypeList = function (type) {
		var logicList = LogicParams.getLogicTypeList(type);
		var html = "<div id=\"logictype" + type + "\" style=\"left:0\" class=\"select-menubutton\">";
		html += "\t<div id=\"logictype" + type + "_child\" class=\"overflow_warp\">";
		html += "\t\t<ul class=\"q-menu-list\">";
		for (var i = 0; i < logicList.length; i++) {
			html += "\t\t\t<li>";
			html += "\t\t\t\t<a class=\"q-menu-item\" href=\"javascript:void(0);\" onclick=\"newLogic(" + logicList[i].type + ");\">" + logicList[i].name + "</a>";
			html += "\t\t\t</li>";
		}
		"\t\t</ul>" + "\t</div>";
		"</div>";
		$("#logicDIVEdit").after(html);
		this.divDropDownPostion("logicAdd_" + type, "logictype" + type, "logictype" + type + "_child");
	};
	
	// 逻辑内添加的下拉选项
	_LogicTool.prototype.showAddTypeList = function (type, flowId) {
		var logicList = LogicParams.getLogicTypeList(type);
		var html = "<div id=\"addlogictype_" + flowId + "\" class=\"select-menubutton\">";
		html += "\t<div id=\"addlogictype_" + flowId + "_child\" class=\"overflow_warp\">";
		html += "\t\t<ul class=\"q-menu-list\">";
		for (var i = 0; i < logicList.length; i++) {
			html += "\t\t\t<li>";
			html += "\t\t\t\t<a class=\"q-menu-item\" href=\"javascript:void(0);\" onclick=\"newLogic(" + logicList[i].type + ");\">" + logicList[i].name + "</a>";
			html += "\t\t\t</li>";
		}
		"\t\t</ul>" + "\t</div>";
		"</div>";
		$("#logicDIVEdit").after(html);
		this.divDropDownPostion("masteradd_" + flowId, "addlogictype_" + flowId, "addlogictype_" + flowId + "_child");
	};
	
	// 详细逻辑问题下拉菜单
	_LogicTool.prototype.showDetailQuestion = function (logicmaster) {
		var detailLogic = logicmaster.logicdetails[0];
		var masteruuid = logicmaster.masteruuid;
		var detailuuid = detailLogic.detailuuid;
		var detailquestionlist = LogicParams.getDetailQuestionList(logicmaster.type);
		
		var detailQuestion = logicmaster.detailQuestion;
		var html = "";
		html += "<div id=\"showlogicdetail_qid_" + masteruuid + "_" + detailuuid + "\" class=\"select-menubutton\">";
		html += "<div id=\"showlogicdetail_qid_" + masteruuid + "_" + detailuuid + "_child\" class=\"overflow_warp\">";
		html += "<ul class=\"q-menu-list\">";
		for (var i = 0; i < detailquestionlist.length; i++) {
			var questionuuid = detailquestionlist[i].questionuuid;
			var fatherquestionuuid = detailquestionlist[i].fatherquestionuuid;
			if(questionuuid == detailQuestion.questionuuid){
				html += "<li class=\"check\">";
			}else{
				html += "<li onclick=\"LogicTool.selectDetailQ('" + logicmaster.sortindex + "','" + questionuuid + "','" + fatherquestionuuid + "');\">";
			}
			html += "<a class=\"q-menu-item\" href=\"javascript:void(0);\">" + this.getLiDivTxt(LogicParams.QUESTION_PREFIX + detailquestionlist[i].sortindex + ".", detailquestionlist[i].description) + "</a>";
			html += "</li>";
		}
		html += "</ul>";
		html += "</div>";
		html += "</div>";
		$("#logicDIVEdit").after(html);
		this.divDropDownPostion("logicdetail_qid_" + masteruuid + "_" + detailuuid, "showlogicdetail_qid_" + masteruuid + "_" + detailuuid, "showlogicdetail_qid_" + masteruuid + "_" + detailuuid + "_child");
	};
	
	// 切换详细问题
	_LogicTool.prototype.selectDetailQ = function(sortindex,questionuuid,fatherquestionuuid) {
		// 从序列中获取逻辑信息
		var logicmaster = LogicParams.logiclist[sortindex - 1];
		var detailLogic = logicmaster.logicdetails[0];

		// 逻辑标识、详细逻辑标识
		var masteruuid = logicmaster.masteruuid;
		var detailuuid = detailLogic.detailuuid;
		// 更改详细逻辑对应问题
		logicmaster.detailQuestion = LogicParams.getDetailQuestion(questionuuid,logicmaster.type);
		detailLogic.answeruuid = logicmaster.detailQuestion.answers[0].answeruuid;
		// 更改逻辑的具体信息
		$("#logicdetaildiv_" + masteruuid + "_" + detailuuid).html($.fn.logicPremise.getDetailOneInnerHtml(logicmaster));
		this.bindDetailClick(logicmaster); // 重新绑定详情事件
		// 如果主逻辑题号小于等于详细逻辑题号;同时调整主逻辑
		if(logicmaster.detailQuestion.sortindex >= logicmaster.masterQuestion.sortindex){
			var masterQuestion = LogicParams.getQuestionBySortindex(logicmaster.detailQuestion.sortindex); // 问题序号从1开始
			logicmaster.masterQuestion = masterQuestion;
			$("#logicmasterinnerdiv_" + masteruuid).html($.fn.logicPremise.getMasterQuestionHtml(logicmaster,logicmaster));
			this.bindMasterClick(logicmaster);
		}
	};
	
	// 详细逻辑答案下拉菜单
	_LogicTool.prototype.showDetailAnswers = function (logicmaster) {
		var detailLogic = logicmaster.logicdetails[0];//详细答案固定为第一个
		var detailquestion = logicmaster.detailQuestion;
		var answerlist = detailquestion.answers;
		var masteruuid = logicmaster.masteruuid;
		var detailuuid = detailLogic.detailuuid;
		var answeruuid = detailLogic.answeruuid;
		var html = "";
		html += "<div id=\"showlogicdetail_answer_" + masteruuid + "_" + detailuuid + "\" class=\"select-menubutton\">";
		html += "<div id=\"showlogicdetail_answer_" + masteruuid + "_" + detailuuid + "_child\" class=\"overflow_warp\">";
		html += "<ul class=\"q-menu-list\">";
		for (var i = 0; i < answerlist.length; i++) {
			if(answerlist[i].answeruuid == answeruuid){
				html += "<li class=\"check\">";
			}else{
				html += "<li onclick=\"LogicTool.selectDetailAnswer('" + logicmaster.sortindex + "','" + answerlist[i].answeruuid + "')\">";
			}
			html += "<a class=\"q-menu-item\" href=\"javascript:void(0);\">" + answerlist[i].name + "</a>";
			html += "</li>";
		}
		html += "</ul>";
		html += "</div>";
		html += "</div>";
		$("#logicDIVEdit").after(html);
		this.divDropDownPostion("logicdetail_answer_" + masteruuid + "_" + detailuuid, "showlogicdetail_answer_" + masteruuid + "_" + detailuuid, "showlogicdetail_answer_" + masteruuid + "_" + detailuuid + "_child");
	};
	
	// 切换详细答案
	_LogicTool.prototype.selectDetailAnswer = function(sortindex,answeruuid) {
		// 从序列中获取逻辑信息
		var logicmaster = LogicParams.logiclist[sortindex - 1];
		var detailLogic = logicmaster.logicdetails[0];
		var masteruuid = logicmaster.masteruuid;
		var detailuuid = detailLogic.detailuuid;
		

		// 更改逻辑的具体信息
		detailLogic.answeruuid = answeruuid;
		
		$("#logicdetaildiv_" + masteruuid + "_" + detailuuid).html($.fn.logicPremise.getDetailOneInnerHtml(logicmaster));
		this.bindDetailClick(logicmaster); // 重新绑定详情事件
	};
	
	// 详细逻辑条件下拉菜单
	_LogicTool.prototype.showDetailCondition = function (logicmaster) {
		var masteruuid = logicmaster.masteruuid;
		var detailLogic = logicmaster.logicdetails[0];
		var detailuuid = detailLogic.detailuuid;
		var condition = detailLogic.condition;
		var conditionlist = LogicParams.getCondtionList(logicmaster.type);
		var html = "";
		html = "<div id=\"showlogicdetail_condition_" + masteruuid + "_" + detailuuid + "\" class=\"select-menubutton\">";
		html += "<div id=\"showlogicdetail_condition_" + masteruuid + "_" + detailuuid + "_child\" class=\"overflow_warp\">";
		html += "<ul class=\"q-menu-list\">";
		for(var i = 0; i < conditionlist.length; i++){
			if(conditionlist[i].value == condition ){
				html += "<li class=\"check\">";
			}else{
				html += "<li onclick=\"LogicTool.selectDetailCoditon('" + logicmaster.sortindex + "','" + conditionlist[i].value + "')\">";
			}
			html += "<a class=\"q-menu-item\" href=\"javascript:void(0);\">" + conditionlist[i].name + "</a>";
			html += "</li>";
		}
		html += "</ul>";
		html += "</div>";
		html += "</div>";
		$("#logicDIVEdit").after(html);
		this.divDropDownPostion("logicdetail_condition_" + masteruuid + "_" + detailuuid, "showlogicdetail_condition_" + masteruuid + "_" + detailuuid, "showlogicdetail_condition_" + masteruuid + "_" + detailuuid + "_child");
	};
	
	// 切换详细逻辑条件
	_LogicTool.prototype.selectDetailCoditon = function(sortindex,condition) {
		// 从序列中获取逻辑信息
		var logicmaster = LogicParams.logiclist[sortindex - 1];
		var detailLogic = logicmaster.logicdetails[0];
		var masteruuid = logicmaster.masteruuid;
		var detailuuid = detailLogic.detailuuid;
		

		// 更改逻辑的具体信息
		detailLogic.condition = condition;
		
		$("#logicdetaildiv_" + masteruuid + "_" + detailuuid).html($.fn.logicPremise.getDetailOneInnerHtml(logicmaster));
		this.bindDetailClick(logicmaster); // 重新绑定详情事件
	};
	
	// 主逻辑问题下拉菜单
	_LogicTool.prototype.showMasterQuestion = function (logicmaster) {
		var masteruuid = logicmaster.masteruuid;
		var detailLogic = logicmaster.logicdetails[0];
		var detailuuid = detailLogic.detailuuid;
		var masterquestionlist = LogicParams.getMasterQuestionList(logicmaster);
		var masterQuestion = logicmaster.masterQuestion;
		var html = "";
		html += "<div id=\"showlogicmaster_qid_" + masteruuid + "\" class=\"select-menubutton\">";
		html += "<div id=\"showlogicmaster_qid_" + masteruuid + "_child\" class=\"overflow_warp\">";
		html += "<ul class=\"q-menu-list\">";
		for (var i = 0; i < masterquestionlist.length; i++) {
			if(masterquestionlist[i].questionuuid == masterQuestion.questionuuid){
				html += "<li class=\"check\">";
			}else{
				html += "<li onclick=\"LogicTool.selectMasterQuestion('" + logicmaster.sortindex + "','" + masterquestionlist[i].questionuuid + "')\">";
			}
			html += "<a class=\"q-menu-item\" href=\"javascript:void(0);\" onclick=\"\">" + this.getLiDivTxt(LogicParams.QUESTION_PREFIX + masterquestionlist[i].sortindex + ".", masterquestionlist[i].description) + "</a>";
			html += "</li>";
		}
		html += "</ul>";
		html += "</div>";
		html += "</div>";
		$("#logicDIVEdit").after(html);
		this.divDropDownPostion("logicmaster_qid_" + masteruuid, "showlogicmaster_qid_" + masteruuid, "showlogicmaster_qid_" + masteruuid + "_child");
	};
	
	// 切换主问题内容
	_LogicTool.prototype.selectMasterQuestion = function(sortindex,questionuuid) {
		var logicmaster = LogicParams.logiclist[sortindex - 1];
		var masteruuid = logicmaster.masteruuid;
		var masterQuestion = LogicParams.getQuestionByUUID(questionuuid);
		
		logicmaster.masterQuestion = masterQuestion;
		logicmaster.questionuuid = masterQuestion.questionuuid;
		logicmaster.fatherquestionuuid = masterQuestion.fatherquestionuuid;
		logicmaster.pageuuid = masterQuestion.pageuuid;
		
		$("#logicmasterinnerdiv_" + masteruuid).html($.fn.logicPremise.getMasterQuestionHtml(logicmaster));
		this.bindMasterClick(logicmaster);
	};
	
	// 隐藏弹出的下拉列表
	_LogicTool.prototype.hideOther = function () {
		$("[id^=logictype]").remove();
		$("[id^=addlogictype_]").remove();
		$("[id^=showlogicdetail_qid_]").remove();
		$("[id^=showlogicdetail_answer_]").remove();
		$("[id^=showlogicmaster_qid_]").remove();
		$("[id^=showlogicdetail_condition_]").remove();
	};
	
	// 判断不为空
	_LogicTool.prototype.isNotEmpty = function (obj) {
		if (obj != undefined && obj != null && obj != "") {
			return true;
		} else {
			return false;
		}
	};
	
	// 获取字符串长度
	_LogicTool.prototype.getStringLength = function (str) {
		var count = 0;
		for (var i = 0; i < str.length; i++) {
			if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) {
				count += 2;
			} else {
				count++;
			}
		}
		return count;
	};
	
	// 去除Html
	_LogicTool.prototype.removeALLHTML = function (html) {
		if (!this.isNotEmpty(html)) {
			return "";
		}
		var result = html;
		result = result.replace(/<[^<]+?>/g, "");
		result = this.replaceALLHTML(result);
		return result;
	};
	
	// 替换html
	_LogicTool.prototype.replaceALLHTML = function (html) {
		if (!this.isNotEmpty(html)) {
			return "";
		}
		var result = html;
		result = result.replace(/\'/g, "&apos;");
		result = result.replace(/\"/g, "&quot;");
		result = result.replace(/</g, "&lt;");
		result = result.replace(/>/g, "&gt;");
		return result;
	};
	
	// 按BYTE截取字符串
	_LogicTool.prototype.getStringByByte = function (str, length) {
		var byteCursor = 0;
		var strCursor = 0;
		var result = "";
		while (byteCursor <= length && strCursor <= str.length) {
			if (str.charCodeAt(strCursor) > 255) {
				byteCursor += 2;
			} else {
				byteCursor += 1;
			}
			if (byteCursor <= length) {
				result += str.substring(strCursor, strCursor + 1);
			}
			strCursor++;
		}
		return result;
	};
	
	// 获取 "前缀" 固定长度的字符 "后缀"
	_LogicTool.prototype.getTxtString = function (length, prefix, str, suffix) {
		var result = "";
		if (this.getStringLength(str) > length) {
			result = getStringByByte(str, length) + "..";
		} else {
			result = str;
		}
		return ((prefix == undefined || prefix == null) ? "" : prefix) + result + ((suffix == undefined || suffix == null) ? "" : suffix);
	};
	
	// 超出长度显示TITLE
	_LogicTool.prototype.getTxtTitleString = function (length, str) {
		if (this.getStringLength(str) > length) {
			return str;
		} else {
			return "";
		}
	};
	
	// 获取问题显示内容 显示最长TEXT_NAME_LENGTH的文字
	_LogicTool.prototype.getDivTxt = function (prefix, description) {
		return this.getTxtString(LogicParams.TEXT_NAME_LENGTH, prefix, description);
	};
	
	// 获取列表中的展示内容 长于TEXT_NAME_LENGTH,显示title,否则，显示
	_LogicTool.prototype.getDivTxtTitle = function (description) {
		return this.getTxtTitleString(LogicParams.TEXT_NAME_LENGTH, description);
	};
	
	// 下拉中文字最长长度
	_LogicTool.prototype.getLiDivTxt = function (prefix, description) {
		return this.getTxtString(LogicParams.LIST_NAME_LENGTH, prefix, description);
	};
	
	// 深度克隆对象
	_LogicTool.prototype.DeepCopy = function (json) {
		if (typeof json == "number" || typeof json == "string" || typeof json == "boolean") {
			return json;
		} else {
			if (typeof json == "object") {
				if (json instanceof Array) {
					var newArr = [], i, len = json.length;
					for (i = 0; i < len; i++) {
						newArr[i] = arguments.callee(json[i]);
					}
					return newArr;
				} else {
					var newObj = {};
					for (var name in json) {
						newObj[name] = arguments.callee(json[name]);
					}
					return newObj;
				}
			}
		}
	};
	
	// 为操作栏按钮绑定内容
	_LogicTool.prototype.bindOneMasterClick =  function(logicmaster){
		var masteruuid = logicmaster.masteruuid;
		var detailLogic = logicmaster.logicdetails[0];
		var detailuuid = detailLogic.detailuuid;
		// 增加逻辑
		$("[id^=masteradd_" + masteruuid + "_]").unbind("click").bind("click",function(evt){
			var type = $(this).attr("id").split("_")[2];
			if ($("#addlogictype_" + masteruuid + "_" + type).length == 0) {
				LogicTool.hideOther();
				LogicTool.showAddTypeList(type, masteruuid + "_"  + type);
			} else {
				LogicTool.hideOther();
			}
			evt.stopPropagation();
		});
	};
	
	// 绑定详情下拉
	_LogicTool.prototype.bindDetailClick = function(logicmaster){
		var masteruuid = logicmaster.masteruuid;
		var detailLogic = logicmaster.logicdetails[0];
		var detailuuid = detailLogic.detailuuid;
		// 详细问题下拉
		$("#logicdetail_qid_" + masteruuid + "_" + detailuuid).unbind("click").bind("click",function(evt){
			if ($("#showlogicdetail_qid_" + masteruuid + "_" + detailuuid).length == 0) {
				LogicTool.hideOther();
				LogicTool.showDetailQuestion(logicmaster);
			} else {
				LogicTool.hideOther();
			}
			evt.stopPropagation();
		});
		$("#logicdetail_condition_" + masteruuid + "_" + detailuuid).unbind("click").bind("click",function(evt){
			//alert("显示条件下拉");
			if ($("#showlogicdetail_condition_" + masteruuid + "_" + detailuuid).length == 0) {
				LogicTool.hideOther();
				LogicTool.showDetailCondition(logicmaster);
			} else {
				LogicTool.hideOther();
			}
			evt.stopPropagation();
		});
		$("#logicdetail_answer_" + masteruuid + "_" + detailuuid).unbind("click").bind("click",function(evt){
			//alert("显示选项下拉");
			if ($("#showlogicdetail_answer_" + masteruuid + "_" + detailuuid).length == 0) {
				LogicTool.hideOther();
				LogicTool.showDetailAnswers(logicmaster);
			} else {
				LogicTool.hideOther();
			}
			evt.stopPropagation();
		});
		
	};
	
	// 绑定主问题点击事件
	_LogicTool.prototype.bindMasterClick = function(logicmaster){
		var masteruuid = logicmaster.masteruuid;
		var detailLogic = logicmaster.logicdetails[0];
		var detailuuid = detailLogic.detailuuid;
		$("#logicmaster_qid_" + masteruuid).unbind("click").bind("click",function(evt){
			//alert("显示主问题下拉");
			if ($("#showlogicmaster_qid_" + masteruuid).length == 0) {
				LogicTool.hideOther();
				LogicTool.showMasterQuestion(logicmaster);
			} else {
				LogicTool.hideOther();
			}
			evt.stopPropagation();
		});
	}
	window.LogicTool = new _LogicTool();
})();

